package b2.player
{
	import b2.config.Config;
	import b2.config.StatusConfig;
//	import b2.net.RRApiHelper;
	import b2.net.ServerEvent;
	import b2.net.ServerHelper;
	
	import flash.events.EventDispatcher;
	
	public class Status extends EventDispatcher
	{
		//玩家本人的ID号唯一号
		public var playerId:int;
		
		public var statusList:Object;
		
		//好友状态索引
		public static const FRIENDS:int = 0;
		
		public static const LOGIN:int = 1;
		
		public function Status(playerId:int)
		{
			this.playerId = playerId;
			statusList = new Object();
			//好友状态
			addFriendStatus();
			
			//签到状态
			addLoginStatus();
			
			dispatchEvent(new StatusEvent(StatusEvent.STATUS_INIT));
		}
		
		private function addFriendStatus():void
		{
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_FRIEND_LIST, onGetFriendList);
			ServerHelper.getInstance().getFriendList(false);
		}
		
		private function onGetFriendList(event:ServerEvent):void
		{
			var friendIds:Array = event.userData as Array;
			if (statusList[FRIENDS] == null)
			{
				addStatusInfo(FRIENDS, friendIds.length);
			}
			else
			{
				updateStatusInfo(FRIENDS, friendIds.length);
			}
		}
		
		public function addStatusInfo(index:int, currentRequireNum:int):void
		{
			if (statusList[index] == null)
			{
				var statusInfo:StatusInfo = new StatusInfo();
				statusList[index] = statusInfo;
				initStatusInfo(index, currentRequireNum);
				var statusEvent:StatusEvent = new StatusEvent(StatusEvent.STATUS_ADD);
				statusEvent.statusIndex = index;
				dispatchEvent(statusEvent);
			}
			
		}

		private function initStatusInfo(index:int, currentRequireNum:int):void
		{
			var statusConfig:StatusConfig = Config.statusConfigs[index];
			var statusInfo:StatusInfo = statusList[index] as StatusInfo;
			statusInfo.statusIndex = index;
			statusInfo.currentRequiredValue = currentRequireNum;
			statusInfo.objectIndex = statusConfig.objectIndex;
			for (var i:int = 0; i<statusConfig.levelConfigs.length; i++)
			{
				if (currentRequireNum < statusConfig.levelConfigs[i].requiredValue)
				{
					
					if (i > 0)
					{
						statusInfo.currentBonus = statusConfig.levelConfigs[i - 1].bonus;
						statusInfo.level = i - 1;
					}
					else
					{
						statusInfo.level = 0;
						statusInfo.currentBonus = 0;
					}
					statusInfo.nextRequiredValue = statusConfig.levelConfigs[i].requiredValue;
					statusInfo.nextBonus = statusConfig.levelConfigs[i].bonus;
					break;
				}
				else if (i == statusConfig.levelConfigs.length - 1)
				{
					statusInfo.currentBonus = statusConfig.levelConfigs[i].bonus;
					statusInfo.level = i;
					statusInfo.nextRequiredValue = statusConfig.levelConfigs[i].requiredValue;
					statusInfo.nextBonus = statusConfig.levelConfigs[i].bonus;
				}
			}
	
		}
		
		public function updateStatusInfo(index:int, currentRequireNum:int):void
		{
			initStatusInfo(index, currentRequireNum);
			var statusEvent:StatusEvent = new StatusEvent(StatusEvent.STATUS_CHANGE);
			statusEvent.statusIndex = index;
			dispatchEvent(statusEvent);
		}
		
		public function deleteStatus(index:int):void
		{
			delete this.statusList[index];
			var statusEvent:StatusEvent = new StatusEvent(StatusEvent.STATUS_DELETE);
			statusEvent.statusIndex = index;
			dispatchEvent(statusEvent);
		}
		
		public function getTotalAddedRange(index:int):int
		{
			var added:int = 0;
			for (var s:String in statusList)
			{
				if ((statusList[s] as StatusInfo).objectIndex == index)
					added += (statusList[s] as StatusInfo).currentBonus;
			}
			return added;
		}
		
		public function addLoginStatus():void
		{
			var statusConfig:StatusConfig = Config.statusConfigs[LOGIN];
			var statusInfo:StatusInfo = new StatusInfo();
			var playerInfo:PlayerInfo = PlayerInfoManager.getInstance().players[playerId];
			var curruent:int = playerInfo.logonDayCount;
			addStatusInfo(LOGIN, curruent);
		}
	}
}